<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="dialogFormVisible = true">
            添加角色
          </el-button>
        </el-col>
      </el-row>

      <el-dialog
        title="添加角色"
        :visible.sync="dialogFormVisible"
        width="50%"
        @close="addClose"
      >
        <el-form :model="form" :rules="addRules" ref="fromRef">
          <el-form-item
            label="角色名称"
            prop="roleName"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.roleName"></el-input>
          </el-form-item>
          <el-form-item
            label="角色描述"
            prop="roleDesc"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="编辑角色"
        :visible.sync="dialogsFormVisible"
        width="50%"
        @close="addClose"
      >
        <el-form :model="form" :rules="addRules" ref="fromRef">
          <el-form-item
            label="角色名称"
            prop="roleName"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.roleName"></el-input>
          </el-form-item>
          <el-form-item
            label="角色描述"
            prop="roleDesc"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogsFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUsers">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="goodsArr" border style="width: 100%" stripe>
        <el-table-column type="index" label="" width="50"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="400">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" width="400">
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="dialogsFormVisible = true"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="open2"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var checkRoleName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('名称不能为空'))
      }
      if (condition) {
      }
    }
    return {
      goodsArr: [],
      dialogFormVisible: false,
      dialogsFormVisible: false,
      form: {
        roleName: '',
        roleDesc: '',
      },
      // 表单验证规则对象
      addRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
        ],
      },
      formLabelWidth: '120px',
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      const res = await this.$http({
        url: 'roles',
        params: {
          query: '',
          pagenum: 1,
          pagesize: 6,
        },
      })
      console.log('用户', res)
      this.goodsArr = res.data.data
    },
    addClose() {
      this.$refs.fromRef.resetFields()
    },
    addUser() {
      this.$message({
        message: '更新信息成功！',
        type: 'success',
      })
      this.dialogFormVisible = false
    },
    addUsers() {
      this.$message({
        message: '更新信息成功！',
        type: 'success',
      })
      this.dialogsFormVisible = false
    },
    open2() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
  },
}
</script>

<style lang="less" scoped>
</style>
